from django.shortcuts import render, HttpResponse
from .models import *
import json, django, datetime
from  django.forms.models import model_to_dict


# Create your views here.
class DateEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, django.db.models.fields.files.ImageFieldFile):
            return str(obj)
        elif isinstance(obj, datetime.datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')


def home(req):
    url = req.get_raw_uri()
    home_date = Home.objects.all()
    nav_all = [model_to_dict(x) for x in home_date]
    for x in nav_all:
        x['home_back'] = url + str(x['home_back'])
    home_date = nav_all[0]

    feature_data = Feature.objects.all()
    fea_all = [model_to_dict(x) for x in feature_data]
    for x in fea_all:
        x['feature_back'] = url + str(x['feature_back'])

    services_data = Services.objects.all()
    ser_all = [model_to_dict(x) for x in services_data]

    section_data = Section.objects.all()
    sec_all = [model_to_dict(x) for x in section_data]
    for x in sec_all:
        x['section_back'] = url + str(x['section_back'])

    footer_data = Footer.objects.all()
    fot_all = [model_to_dict(x) for x in footer_data][0]
    return render(req, 'index.html', {'home_date': home_date, 'fea_date': fea_all, 'ser_date': ser_all,
                                      'sec_date': sec_all,
                                      'fot_all':fot_all})
